import dt from '@dt/dt'
import orderApi from '@/config/server/api/order.js'
import userApi from '@/config/server/api/user.js'
import { Message } from 'view-design'

let search = null

function list(info) {
  info.housesId = dt.buildInfo().housesId
  return orderApi.bonus.template.list.fetch(info)
}

function listAll(info) {
  return orderApi.bonus.template.listAll.fetch(info)
}

function detail(templateId) {
  return orderApi.bonus.template.detail.fetch({ templateId }).then(res => {
    return {
      ...res,
      bonusTemplateDetailInfos: res.bonusTemplateDetailInfos.map(item => {
        item.fixedAmount = Number(item.fixedAmount)
        let obj = {}
        if (res.templateType == 'PRE') {
          obj.settlementType = item.settlement[0].settlementType
          obj.settlementProportion = item.settlement[0].settlementProportion
        } else {
          for (let i of item.settlement) {
            obj[i.settlementType] = i.settlementProportion
          }
        }
        return { ...item, ...obj }
      })
    }
  })
}

function save(info) {
  info.housesId = dt.buildInfo().housesId
  return orderApi.bonus.template.save.fetch({
    ...info,
    bonusTemplateDetailInfos: info.bonusTemplateDetailInfos.map(item => {
      return {
        detailName: item.detailName,
        postIds: item.postIds,
        issueType: item.issueType,
        scaleType: item.issueType == '1' ? item.scaleType : '',
        fixedAmount: item.issueType == '2' ? item.fixedAmount : '',
        settlement: info.templateType == 'PRE' ? [{
          settlementType: item.settlementType,
          settlementProportion: item.settlementProportion
        }] : dt.dictInfo().bonusSettlementType.map(i => ({
          settlementType: i.key,
          settlementProportion: item[i.key]
        }))
      }
    })
  })
}

function repeat(name) {
  return orderApi.bonus.template.repeat.fetch({
    housesId: dt.buildInfo().housesId,
    name
  })
}

function del(templateId) {
  return orderApi.bonus.template.del.fetch({ templateId })
}

// 获取参与奖金的岗位列表
function bonusPosition() {
  return userApi.jobPostManagement.bonusPosition.fetch().then(res => {
    let data = res.map(i => ({
      key: i.id,
      value: i.positionName
    }))
    return data
  })
}

function validateNum(val) {
  if (!val && val !== 0) {
    return false
  }
  return true
}

function validateDetail(list, templateType) {
  let flag = true, reg = /^\d+$/
  if (list.length <= 0) {
    Message.error('请添加奖金模板明细')
    return false
  } else {
    let msg = '请填写完整奖金模板明细'
    for (let item of list) {
      if (
        !item.detailName || !item.postIds.length || (item.issueType == '2' && !validateNum(item.fixedAmount))
      ) {
        flag = false
        break
      } else if (templateType == 'PRE' && item.issueType == '1' && !reg.test(item.settlementProportion)) {
        flag = false
        msg = '结算比例应为整数'
        break
      } else if (templateType != 'PRE') {
        if (!reg.test(item['REBATE']) || !reg.test(item['YEAR_END']) || !reg.test(item['HANDOVER'])) {
          flag = false
          msg = '结算比例应为整数'
          break
        } else if (item['REBATE'] + item['YEAR_END'] + item['HANDOVER'] != 100) {
          flag = false
          msg = '结算条件三项比例之和必须为100'
          break
        }
      }
    }
    if (!flag) {
      Message.error(msg)
      return false
    }
  }
  return true
}

function saveSearch(params) {
  search = params
}

function getSearch(params) {
  return search
}

export default {
  list,
  listAll,
  detail,
  save,
  repeat,
  del,
  
  bonusPosition,
  validateDetail,
  saveSearch,
  getSearch
}